package com.example.wechar.contorller;

import com.example.wechar.utils.CheckUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class LoginController {

    @GetMapping("/wx")
    @ApiOperation("登录方法")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("success");
        String signature = request.getParameter("signature");
        System.out.println("signature=" + signature);

        String timestamp = request.getParameter("timestamp");
        System.out.println("timestamp=" + timestamp);

        String nonce = request.getParameter("nonce");
        System.out.println("nonce=" + nonce);

        String echostr = request.getParameter("echostr");
        System.out.println("echostr=" + echostr);

        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
                out.write(echostr);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            out.close();
        }

    }
}

